package com.ffapp.rainmonitorcustom.gis;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.alarm.AlarmSearchActivity;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.basis.MyViewPagerAdapter;
import com.ffapp.rainmonitorcustom.bean.IndexInfoBean;
import com.ffapp.rainmonitorcustom.login.LoginActivity;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import my.ActivityTool;
import my.DialogUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 地图
 * */
public class GisBaseActivity extends BasisActivity {

    @BindView(R.id.tablayout)
    CommonTabLayout tablayout;
    @BindView(R.id.eidt_user)
    EditText mEditText;
    @BindView(R.id.tv_device_num)
    TextView mDeviceNum;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    MyViewPagerAdapter myViewPagerAdapter;


    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    Fragment[] fms;

    @Override
    public void initViews() {

//        setDefautTrans(false);
        setContentView(R.layout.user_msg_tab_base_activity);
        super.initViews();
        setTitle("水情信息检测平台APP");


        setTitleRightButton(R.mipmap.exit_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.DialogTwo(mContext, "", "退出当前账号？", "立即退出", "取消", new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        LoginActivity.ToLogin(mContext);
                        finish();
                    }
                },null);
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setView();
        getList();
    }

    private void getList() {
        RequestParams params = new RequestParams();
        showProgress();
        HttpRestClient.get(Constants.URL_INDEX_INFO, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        IndexInfoBean bean = (IndexInfoBean) result;
                        mDeviceNum.setText("共"+bean.getOnlineCount()+"/"+bean.getAllCount()+"台设备");
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, IndexInfoBean.class);
    }

    private  void setView(){

         fms = new Fragment[2];
//        HomeGrideListData data = new HomeGrideListData();
//        data.setModuleCode("XF_BFM_WATER");
        fms[0] = GisListFragment.newInstance();
        fms[1] =MapFragment.newInstance();
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(),fms);
        viewpager.setAdapter(myViewPagerAdapter);
        viewpager.setOffscreenPageLimit(2);
        mTabEntities = new ArrayList<>();
        mTabEntities.add(new TabEntity("列表展示",0,0));
        mTabEntities.add(new TabEntity("地图展示",0,0));

       String[] listTitle = new String[2];

        listTitle[0] = "告警消息";
//        listTitle[1] = "普通消息";
        tablayout.setTabData(mTabEntities);
//        tablayout.setViewPager(viewpager,  listTitle);

        tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tablayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewpager.setCurrentItem(0);
    }


    public class TabEntity implements CustomTabEntity {
        public String title;
        public int selectedIcon;
        public int unSelectedIcon;

        public TabEntity(String title, int selectedIcon, int unSelectedIcon) {
            this.title = title;
            this.selectedIcon = selectedIcon;
            this.unSelectedIcon = unSelectedIcon;
        }

        @Override
        public String getTabTitle() {
            return title;
        }

        @Override
        public int getTabSelectedIcon() {
            return selectedIcon;
        }

        @Override
        public int getTabUnselectedIcon() {
            return unSelectedIcon;
        }
    }


//    @OnClick(R.id.rl_search_icon)
//    public void search(){
//        EventBus.getDefault().post(new MessageEvent(mEditText.getText().toString().trim()));
//    }

    @OnClick(R.id.eidt_user)
    public void toSearchActivity(){
        ActivityTool.skipActivity(mContext, AlarmSearchActivity.class);
    }
}
